import 'package:easy_read_comic/controller/book/book_controller.dart';
import 'package:easy_read_comic/view/comic/detail_view.dart';
import 'package:easy_read_comic/widgets/auto_image_widget.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class BookView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: Align(
              child: Text(
                "书架",
                style: TextStyle(
                  color: Colors.white,
                ),
              ),
              alignment: Alignment.centerLeft,
            ),
            backgroundColor: Theme.of(context).primaryColor,
            actions: [
              IconButton(
                onPressed: () {},
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
              ),
            ],
          ),
          body: EasyRefresh(
            onLoad: () {
              controller.loadMore();
            },
            onRefresh: () {
              controller.resetLoad();
            },
            child: Container(
              padding: EdgeInsets.only(
                top: 10,
                left: 10,
                right: 10,
              ),
              child: GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  mainAxisSpacing: 10.0,
                  crossAxisSpacing: 10.0,
                  childAspectRatio: 0.6,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    child: Container(
                      child: Column(
                        children: [
                          ClipRRect(
                            child: AutoImage.buildImage(
                              controller.list[index].Thumb,
                            ),
                            borderRadius: BorderRadius.circular(10),
                          ),
                          Container(
                            child: Text(
                              controller.list[index].Title,
                              style: TextStyle(fontSize: 15),
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                            padding: EdgeInsets.only(
                              top: 5,
                            ),
                          ),
                        ],
                        crossAxisAlignment: CrossAxisAlignment.start,
                      ),
                    ),
                    onTap: () {
                      Get.toNamed("/detail/${controller.list[index].Id}");
                    },
                  );
                },
                itemCount: controller.list.length,
              ),
            ),
          ),
        );
      },
      init: BookController(),
    );
  }
}
